/*	
 *				
 *					ISNW ---- It is a war.It is not warcraft!
 *
 *
 *					Author:					zhoukeli(haxixi_keli)
 *					Filename:				BMPImageLoader.h
 *					CreateTime:				2008/12/31
 *					ModifyTime:				2008/12/31
 *
 */

#ifndef __ISNW_IMAGE_LOADER_H__
#define __ISNW_IMAGE_LOADER_H__

#include "include/IImageLoader.h"
#include "Image.h"
#include "include/isnwString.h"

namespace ISNW
{
namespace video
{

// byte aligned structure
#if defined(_MSC_VER)
#	pragma pack( push, packing )
#	pragma pack(1)
#else
#	error compiler not support
#endif

	struct BMPHeader
	{
		u16			Type;			//	BM for windows XP, NT, 2000, ME, we will ignore other types
									//	BA - OS/2 Bitmap Array
									//	CI - OS/2 Color Icon
									//	CP - OS/2 Color Pointer
									//	IC - OS/2 Icon
									//	PT - OS/2 Pointer

		u32			Size;			// size of the bmp file

		u32			Reserved;

		u32			Offbits;		// offset from the beginning of the header to bitmap bits

		u32			InfoHeadersize;	// size of infoheader

		s32			Width;			// width , in pixels

		s32			Height;			// height, in pixels

		u16			Planes;			// must be 1

		u16			BPP;			// bits-per-pixels
									// 0 --- JPEG or PNG format
									// 1 --- the bitmap is monochrome
									// 4 --- the bitmap has a maximum of 16 colors
									// 8 --- the bitmap has a maximum of 256 colors
									// 16 --- 5-5-5 or 5-6-5
									// 24 --- 8-8-8
									// 32 --- A-R-G-B : 8-8-8-8

		u32			Compression;	// BI_RGB --- uncompressed format
									// BI_RLE8 --- run-length encoded for 8 bpp, count(8-bits)-index(8bits)
									// BI_RLE4 --- run-length encoded for 4 bpp
									// BI_BITFIELDS --- the color table consists of color masks
									// BI_JPEG --- jpeg compressed file
									// BI_PNG --- png compressed file

		u32			SizeImage;		// specifies the size, in bytes, of the image. may be 0 for BI_RGB

		s32			XPelsPerMeter;

		s32			YPerlsPerMeter;

		u32			ClrUsed;

		u32			ClrImportant;

		u32			Colors[1];		// RGBQUAD arrays, it may contain color masks in 16 bpp

	};

#if	defined(_MSC_VER)
#	pragma pop( packing )
#endif



	class BMPImageLoader : public IImageLoader
	{
	public:
		BMPImageLoader():image(0),downTop(true){}

	public:
		virtual IImage* loadImage(io::IReadFile*);

		virtual bool isLoaderableFile(const c8*) const;

	protected:
		// 16 bpp need color masks
		void load16(io::IReadFile*,u16 redMask,u16 greenMask,u16 blueMask);

		void load24(io::IReadFile*);

		void load32(io::IReadFile*);

	protected:
		Image*	image;

		bool downTop;
	};
} // end namespace video
} // end namespace ISNW
#endif